import { autorun, makeAutoObservable } from 'mobx';
import { createContext } from 'react';

import UserStore from '@/store/user';

const ROOT_STORE = 'DEMO';

class RootStore {
  userStore; //用户信息
  isLoggedIn: boolean = false;

  constructor(props: any) {
    props && Object.assign(this, props);
    makeAutoObservable(this);
    this.userStore = new UserStore();
  }
}

//save in localStorage
const store = JSON.parse(localStorage.getItem(ROOT_STORE) || '{}');
const root = new RootStore(store);
autorun(() => {
  localStorage.setItem(ROOT_STORE, JSON.stringify(root));
});

//public
export const rootStore = root;
const Context = createContext(root);
export default Context;
